<template>
  <el-card>
    <div slot="header" class="header">
      <div class="category-header">
        <span>销售额类别占比</span>
        <el-radio-group v-model="value">
          <el-radio-button label="全部渠道" />
          <el-radio-button label="线上" />
          <el-radio-button label="门店" />
        </el-radio-group>
      </div>
    </div>
    <div>
      <div ref="charts" class="charts" />
    </div>
  </el-card>
</template>

<script>
import * as echarts from 'echarts'
export default {
  name: '',
  data() {
    return {
      value: '全部渠道',
      data: [
        { value: 300, name: '视频广告' },
        { value: 735, name: '搜索引擎' },
        { value: 580, name: '联盟广告' },
        { value: 484, name: '邮件销售' },
        { value: 300, name: '直接访问' }
      ]
    }
  },
  mounted() {
    // 饼图
    const mychart = echarts.init(this.$refs.charts)
    mychart.setOption({
      title: {
        text: this.data[0].name,
        subtext: this.data[0].value,
        left: 'center',
        top: 'center'
      },
      tooltip: {
        trigger: 'item'
      },
      series: [
        {
          name: 'Access From',
          type: 'pie',
          radius: ['40%', '70%'],
          avoidLabelOverlap: false,
          itemStyle: {
            borderRadius: 10,
            borderColor: '#fff',
            borderWidth: 2
          },
          label: {
            show: true,
            position: 'outside', // 修改为 'outside'
            alignTo: 'labelLine', // 使标签对齐到 labelLine
            bleedMargin: 4 // 设置标签的出血边距，确保标签和图形之间有一定距离
          },
          labelLine: {
            show: true,
            length: 30, // 设置连接线的长度
            smooth: 0 // 设置连接线的曲度，取值范围为 [0, 1]
          },
          data: this.data
        }
      ]
    })

    // 绑定事件
    mychart.on('mouseover', (params) => {
      // 获取鼠标移上去那条数据
      const { name, value } = params.data
      // 重新设置标题
      mychart.setOption({
        title: {
          text: name,
          subtext: value
        }
      })
    })
  }
}
</script>

<style scoped>
.category-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.header {
  border-bottom: 1px solid #eee;
}
.charts {
  width: 100%;
  height: 300px;
}
</style>

